package cs757;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

public class LoginGUI extends JDialog {

	private static final long serialVersionUID = -5408460651711568002L;

	private DatabaseConnector dbConn;
	
	private JPanel contentPane;
	private JTextField textFieldUserName;
	
	private String userName;
	private JPasswordField passwordField;
	
	private boolean ok = false;
	private JTextField textFieldServer;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginGUI frame = new LoginGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginGUI() {
		setTitle("Login");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 333, 231);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblLoginToUniversity = new JLabel("Login to University Database");
		lblLoginToUniversity.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 18));
		
		textFieldUserName = new JTextField();
		textFieldUserName.setColumns(10);
		
		JLabel lblUserName = new JLabel("User name:");
		
		JLabel lblPassword = new JLabel("Password:");
		
		JButton btnLogin = new JButton("Login!");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Connection conn = loginToDatabase();
				if (conn != null){
					dbConn.setConn(conn); 
					dbConn.setUserID(userName);
					ok = true;
					closeDialog();
				}
				else {
					JOptionPane.showMessageDialog(null, "Error connecting to database.\nPlease check your username and password, and try again.",
							"Error Logging In", JOptionPane.ERROR_MESSAGE);
				}

			}
		});
		
		passwordField = new JPasswordField();
		passwordField.setText("");
		passwordField.setColumns(10);
		
		textFieldServer = new JTextField();
		textFieldServer.setText("");
		textFieldServer.setColumns(10);
		
		JLabel lblServer = new JLabel("Server:");
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addComponent(lblServer)
								.addComponent(lblPassword)
								.addComponent(lblUserName))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
								.addComponent(textFieldUserName)
								.addComponent(passwordField)
								.addComponent(textFieldServer, GroupLayout.PREFERRED_SIZE, 183, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(btnLogin)
							.addGap(38)))
					.addGap(84))
				.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
					.addGap(18)
					.addComponent(lblLoginToUniversity)
					.addContainerGap(61, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addContainerGap(20, Short.MAX_VALUE)
					.addComponent(lblLoginToUniversity)
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(textFieldServer, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblServer))
					.addGap(9)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(textFieldUserName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblUserName))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblPassword)
						.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addComponent(btnLogin)
					.addContainerGap())
		);
		contentPane.setLayout(gl_contentPane);
	}
	
	/**
	 * Launch the GUI to login to the database and return the user ID to the DatabaseConnector
	 * @param dbConn
	 */
	public void launchLoginWindow(DatabaseConnector dbConn){
		this.dbConn = dbConn;
		this.setVisible(true);
	}
	
	/**
	 * Close the login GUI
	 */
	private void closeDialog(){
		this.setVisible(false);
	}
	
	/**
	 * Try to login
	 */
	private Connection loginToDatabase(){
		userName = textFieldUserName.getText();
		String password = new String(passwordField.getPassword());
		String server = textFieldServer.getText();
		
		return dbConn.getConnection(userName, password, server);

	}
	

	/**
	 * @return the ok
	 */
	public boolean isOk() {
		return ok;
	}
}
